package net.vkits.api;

import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiParam;
import net.vkits.anjiamiao.application.CustomerApplicationService;
import net.vkits.anjiamiao.application.VerifyCodeApplicationService;
import net.vkits.common.dto.BaseDTO;
import net.vkits.common.dto.JwtToken;
import net.vkits.common.dto.LoginUser;
import net.vkits.common.dto.User;
import net.vkits.common.exceptions.NoSuchVerifyCode;
import net.vkits.common.filter.JwtHelper;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by ethan-liu on 2016/11/5.
 */

@Controller
public class UserApiController implements UserApi {

    @Resource
    private HttpServletRequest request;
    @Resource
    private CustomerApplicationService customerApplicationService;
    @Resource
    private VerifyCodeApplicationService verifyCodeApplicationService;

    @Override
    public ResponseEntity<BaseDTO> changeCellphoneNumber(@ApiParam(value = "更换手机号码", required = true) @RequestBody LoginUser body) {
        final Claims claims = (Claims) request.getAttribute("claims");
        String cellphone = claims.get("phone").toString();
        customerApplicationService.changeCellphoneNum(cellphone, body.getCellPhone(), body.getVerifyCode());
        BaseDTO baseDTO = new BaseDTO();
        baseDTO.setCode(200);
        baseDTO.setMsg("OK");
        return new ResponseEntity<>(baseDTO, HttpStatus.OK);
    }

    @Override
    public ResponseEntity<User> getUser() {
        final Claims claims = (Claims) request.getAttribute("claims");
        String cellphone = claims.get("phone").toString();
        User user = customerApplicationService.getUser(cellphone);
        return new ResponseEntity<>(user, HttpStatus.OK);
    }

    @Override
    public ResponseEntity<BaseDTO> deleteUser() {
        final Claims claims = (Claims) request.getAttribute("claims");
        String cellphone = claims.get("phone").toString();
        customerApplicationService.deleteUser(cellphone);
        BaseDTO baseDTO = new BaseDTO();
        baseDTO.setCode(200);
        baseDTO.setMsg("OK");
        return new ResponseEntity<>(baseDTO, HttpStatus.OK);
    }

    @Override
    public ResponseEntity<BaseDTO> updateUser(@ApiParam(value = "Updated user name", required = true) @RequestParam(value = "name", required = true) String name, @ApiParam(value = "Updated user id number", required = true) @RequestParam(value = "idnumber", required = true) String idnumber) {
        final Claims claims = (Claims) request.getAttribute("claims");
        String cellphone = claims.get("phone").toString();
        customerApplicationService.updateUser(cellphone, name, idnumber);
        BaseDTO baseDTO = new BaseDTO();
        baseDTO.setCode(200);
        baseDTO.setMsg("OK");
        return new ResponseEntity<>(baseDTO, HttpStatus.OK);
    }

    @Override
    public ResponseEntity<BaseDTO> userAvatarUploadPost(@ApiParam(value = "file detail") @RequestPart("file") MultipartFile file) {
        final Claims claims = (Claims) request.getAttribute("claims");
        String cellphone = claims.get("phone").toString();
        customerApplicationService.uploadAvatar(file, cellphone);
        BaseDTO baseDTO = new BaseDTO();
        baseDTO.setCode(200);
        baseDTO.setMsg("OK");
        return new ResponseEntity<>(baseDTO, HttpStatus.OK);
    }

    @Override
    public ResponseEntity<BaseDTO> userIdcardUploadPost(@ApiParam(value = "file detail") @RequestPart("file1") MultipartFile file1, @ApiParam(value = "file detail") @RequestPart("file2") MultipartFile file2) {
        final Claims claims = (Claims) request.getAttribute("claims");
        String cellphone = claims.get("phone").toString();
        customerApplicationService.uploadIdPhotos(file1, file2, cellphone);
        BaseDTO baseDTO = new BaseDTO();
        baseDTO.setCode(200);
        baseDTO.setMsg("OK");
        return new ResponseEntity<>(baseDTO, HttpStatus.OK);
    }

    @Override
    public ResponseEntity<JwtToken> loginUser(@ApiParam(value = "The user obj for login", required = true)
                                              @RequestBody LoginUser body) {
        boolean result = customerApplicationService.login(body.getCellPhone(), body.getVerifyCode());
        if (result) {
            JwtToken jwtToken = new JwtToken();
            jwtToken.setJwtToken(JwtHelper.createJWT(body.getCellPhone()));
            return new ResponseEntity<>(jwtToken, HttpStatus.OK);
        }
        throw new NoSuchVerifyCode();
    }

    @Override
    public ResponseEntity<BaseDTO> userSmscodeGet(@ApiParam(value = "发送验证码", required = true)
                                               @RequestParam(value = "cellPhone", required = true) String cellPhone) {
        verifyCodeApplicationService.sendSmsCode(cellPhone);
        BaseDTO baseDTO = new BaseDTO();
        baseDTO.setCode(200);
        baseDTO.setMsg("OK");
        return new ResponseEntity<>(baseDTO, HttpStatus.OK);
    }
}
